import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../prisma/prisma.service';
import { Family } from '@prisma/client';
import { CreateFamilyDto } from '../dto/create-family.dto';

@Injectable()
export class FamilyService {
    constructor(private prisma: PrismaService) {}
    async getFamily(userId: number) {
        return await this.prisma.family.findMany({
            where: {
                user_id: userId
            }
        });
    }

    async setFamily(userId: number, family: CreateFamilyDto) {
        const { id , ...familyData } = family;
        return this.prisma.family.upsert({
            where: {
                id: id ?? -1,
                user_id: userId
            },
            create: {
                ...familyData,
                user_id: userId
            },
            update: {
                ...familyData,
                user_id: userId
            }
        });
    }

    async deleteFamily(userId: number, id: number) {
        return this.prisma.family.delete({
            where: {
                id,
                user_id: userId
            }
        });
    }
}
